from fastapi import APIRouter, Depends, Request, Query
from app.schema.sys.user import UserLogin
from utils.response import success, fail, tableinfo
from sqlalchemy.orm import Session
from utils.database import get_db
from utils.redis import RedisUtil
from app.model import ActivityModel, ActivityApplyModel
from app.log import Log
import uuid, json
from utils.request import PageInfo, QueryWrapper

activityController = APIRouter()


@activityController.get("/page")
async def get_list( page:int=Query(default=1), limit:int=Query(default=10), filter:str=Query(default='{}'), db: Session = Depends(get_db)):
    page  = PageInfo(page, limit)
    where = QueryWrapper.build_query_wrapper(filter)
    [total, rows] = ActivityModel.get_page(db, where, page)
    return tableinfo("查询成功", total, rows)


@activityController.get("/get")
async def get_dept(id:int,  db: Session = Depends(get_db)):
    post = ActivityModel.get_by_id(db, id)
    return success("操作成功", post)


@activityController.get("/apply")
async def apply(req:Request, id:int,  db: Session = Depends(get_db)):
    
    data = {
        "act_id": id,
        "user_id":req.app.state.user_id
    }
    try:
        ActivityApplyModel.insert(db, data)
    except:
        return fail("预约失败")

    return success("操作成功")